package java_class_work.week08.internet.sendStringDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @Author Tiam
 * @Date 2022/11/6 22:06
 * @Description: 读取消息
 */
public class ClientThread implements Runnable {
    Socket socket;

    public ClientThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        boolean isExit = true;
        while (isExit) {
            try {
                readMessage();
            } catch (Exception e) {
                System.out.println("服务器断开连接");
                isExit = false;
                System.exit(-1);
            }
        }
    }

    /**
     * 线程: 读取其他用户的消息
     */
    private void readMessage() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("\n正在等待返回消息... ");
        String message = in.readLine();
        System.out.println(message);
    }

}
